package com.example.dollarShipping.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dollarShipping.entity.*;
import com.example.dollarShipping.mapper.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShippingOrderService {
    @Resource
    ShippingOrderMapper shippingOrderMapper;
    @Resource
    OperationRecordMapper operationRecordMapper;
    @Resource
    TruckOrderMapper truckOrderMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    TruckMapper truckMapper;

    public List<ShippingOrder> getShippingOrder(Long creatorId, Integer state, Long carrierId) {
        QueryWrapper wrapper = new QueryWrapper();
        if(creatorId!=null) wrapper.eq("creator_id", creatorId);
        if(state!=null) wrapper.eq("state", state);
        if(carrierId!=null) wrapper.eq("carrier_id", carrierId);
        List<ShippingOrder> shippingOrder = shippingOrderMapper.selectList(wrapper);
        shippingOrder.stream().map(s -> {
            List<OperationRecord> sl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",1).eq("order_id",s.getId()));
            List<TruckOrder> truckOrder = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
            for(TruckOrder t:truckOrder){
                List<OperationRecord> tl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",2).eq("order_id",t.getId()));
                t.setOperationRecords(tl);
            }
            s.setOperationRecords(sl);
            s.setTruckOrder(truckOrder);
            return s;
        }).collect(Collectors.toList());
        return shippingOrder;
    }

    public void createOrder(ShippingOrder s) {
        shippingOrderMapper.insert(s);
    }

    public void updateOrder(ShippingOrder s) {
        shippingOrderMapper.updateById(s);
    }



    public void delete(Long id) {
        shippingOrderMapper.deleteById(id);
    }
    public ShippingOrder findShippingOrder(Long orderId) {
        ShippingOrder s = shippingOrderMapper.selectById(orderId);
        return moreOrderDetail(s);
    }
    private ShippingOrder moreOrderDetail(ShippingOrder s){
        List<OperationRecord> sl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",1).eq("order_id",s.getId()));
        List<TruckOrder> truckOrder = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        for(TruckOrder t:truckOrder){
            User driver =userMapper.selectById(t.getDriverId());
            Truck truck = truckMapper.selectById(t.getTruckId());
            t.setDriver(driver.getRealName());
            String truckName = truck.getNumber()+"(dwt:"+truck.getWeight()+")";
            t.setTruck(truckName);
            List<OperationRecord> tl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",2).eq("order_id",t.getId()));
            t.setOperationRecords(tl);
        }
        s.setOperationRecords(sl);
        s.setTruckOrder(truckOrder);
        return s;
    }


    public ShippingOrder selectById(Long id) {
        return shippingOrderMapper.selectById(id);
    }
}
